package asapIs134CK.womi.client.games.mathEnigme;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class ArithmeticQuizz extends Composite{
	public static final int VERYEASY = 0;
	public static final int EASY = 1;
	public static final int NORMAL = 2;
	public static final int HARD = 3;
	public static final int VERYHARD = 4;
	
	private ArithmeticSequence _parent;
	private Label question;
	private final TextBox answer;
	private Button submit;
	private FlowPanel panel;
	private int _difficulty;
	private int _result;
	
	public ArithmeticQuizz(int difficulty, ArithmeticSequence parent){
		_parent = parent;
		_difficulty = difficulty;
		panel = new FlowPanel();
		panel.setStyleName("ArithmeticQuizz");
		question = new Label(generateQuestion());
		answer = new TextBox();
		ClickHandler submitClickHandler = new ClickHandler(){
			public void onClick(ClickEvent event) {
				_parent.nextGame(isCorrect());
			}		
		};
		submit = new Button("Submit", submitClickHandler);
		KeyUpHandler answerEnterKeyUpHandler = new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					_parent.nextGame(isCorrect());
					//TODO Later : fix this dubble code by one method action 
					//called in both of this handlers
				}
			}
		};
		
		//set the focus one the answer field
		answer.setText("answer here !");
		answer.setFocus(true);
		answer.selectAll();
		
		answer.addKeyUpHandler(answerEnterKeyUpHandler);
		panel.add(question);
		panel.add(answer);
		panel.add(submit);
		
		
		initWidget(panel);
	}

	private String generateQuestion() {
		char operator = generateOperator();
		int firstOperand = generateOperand(operator);
		int secondOperand = generateOperand(operator);
		_result = calculateResult(operator, firstOperand, secondOperand);
		return firstOperand+" "+operator+" "+secondOperand+" =";
	}

	private int calculateResult(char operator, int firstOperand,
			int secondOperand) {
		int res = 0;
		switch (operator) {
		default : case '+':
			res = firstOperand + secondOperand;
			break;

		case '-':
			res = firstOperand - secondOperand;
			break;
		case '*':
			res = firstOperand * secondOperand;
			break;
		}
		return res;
	}

	private int generateOperand(char operator) {
		int res = 0;
		int bound = 12;
		if(operator != '*'){
			switch(_difficulty){
			default : case VERYEASY :
				bound = 20;
				break;
			case EASY : 
				bound = 50;
				break;
			case NORMAL : 
				bound = 100;
				break;
			case HARD : 
				bound = 500;
				break;
			case VERYHARD : 
				bound = 1000;
				break;
			}
		}
		res = Random.nextInt(bound);		
		return res;
	}

	private char generateOperator() {
		char[] operators = {'+', '-', '*'};
		int rand = 0;
		char res;
		switch(_difficulty){
		default :case VERYEASY : case EASY :
			res = operators[rand];
			break;
			
		case NORMAL :
			rand = Random.nextInt(2);
			res = operators[rand];
			break;
			
		case HARD : case VERYHARD :
			rand = Random.nextInt(3);
			res = operators[rand];
			break;
		}
		return res;
	}
	
	private Boolean isCorrect(){
		boolean res = false;
		boolean negative = false;
		String stringToParse = answer.getText();
		int ans = 0;
		
		if(stringToParse.startsWith("-")){
			stringToParse = stringToParse.substring(1);
			negative = true;
		}
		try {
			ans =Integer.parseInt(stringToParse);
			if(negative){
				ans -= 2*ans;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		if(ans == _result){
			res = true;
		}
		return res;		
	}

	public void setFocus(boolean b) {
		
		answer.setFocus(b);
		if(b){
			answer.selectAll();
		}
		//System.out.println("ArithQuizz:setFocus done");
	}
}
